#include "sem.hpp"
#include "task.hpp"
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
void *ProductorRoutine(void *rq)
{
    RingQueue<Task> *ringqueue = static_cast<RingQueue<Task> *>(rq);
    while (true)
    {
        int x = rand() % 10;
        int y = rand() % 5;
        char op = oper[rand() % oper.size()];
        Task t(x, y, op, mymath);
        ringqueue->Push(t);
        printf("生产者派发了一个任务: %s\n", t.toTaskString().c_str());
        // sleep(1);
    }
}
void *ConsumerRoutine(void *rq)
{
    RingQueue<Task> *ringqueue = static_cast<RingQueue<Task> *>(rq);
    while (true)
    {
        Task t;
        ringqueue->Pop(&t);
        std::string result = t();
        printf("消费者消费了一个任务: %s\n", result.c_str());
        sleep(1);
    }
}
int main()
{
    srand((unsigned int)time(nullptr) ^ getpid() ^ pthread_self() ^ 0x71727374);
    RingQueue<Task> *rq = new RingQueue<Task>();
    pthread_t p, c;
    pthread_create(&p, nullptr, ProductorRoutine, rq);
    pthread_create(&c, nullptr, ConsumerRoutine, rq);
    pthread_join(p, nullptr);
    pthread_join(c, nullptr);
    delete rq;
    return 0;
}